from models import *
from django.contrib.auth.models import User

# get service info


def get_flight_info(flight_id):
    flights = Flight.objects.filter(flight_id=flight_id)
    if flights.count() > 0:
        return flights[0]
    else:
        return '_no_this_service'


def get_bus_info(bus_id):
    buses = Bus.objects.filter(bus_id=bus_id)
    if buses.count() > 0:
        return buses[0]
    else:
        return '_no_this_service'


def get_hotel_info(hotel_id):
    hotels = Hotel.objects.filter(hotel_id=hotel_id)
    if hotels.count() > 0:
        return hotels[0]
    else:
        return '_no_this_service'


def get_location_info(location_id):
    locations = Location.objects.filter(loacation_id=location_id)
    if locations.count() > 0:
        return locations[0].location_introduction
    else:
        return '_no_this_location'


# get reservation


def get_flight_reserve(flight_id):
    flights = Flight.objects.filter(flight_id=flight_id)
    if not flights.count() > 0:
        return '_no_this_service'
    flight = flights[0]
    return flight.reserve.count()


def get_bus_reserve(bus_id):
    buses = Bus.objects.filter(bus_id=bus_id)
    if not buses.count() > 0:
        return '_no_this_service'
    bus = buses[0]
    return bus.reserve.count()


def get_hotel_reserve(hotel_id):
    hotels = Hotel.objects.filter(hotel_id=hotel_id)
    if not hotels.count() > 0:
        return '_no_this_service'
    hotel = hotels[0]
    return hotel.reserve.count()


def get_user_reserve(user_id):
    users = User.objects.filter(id=user_id)
    user = users[0]
    if user is None:
        return '_no_this_user'
    flight_reserve = user.flight_reserve.all()
    bus_reserve = user.bus_reserve.all()
    hotel_reserve = user.hotel_reserve.all()
    # transfer
    flight_reserve_set = []
    for item in flight_reserve:
        flight_reserve_set.append(item.flight_id)
    bus_reserve_set = []
    for item in bus_reserve:
        bus_reserve_set.append(item.bus_id)
    hotel_reserve_set = []
    for item in hotel_reserve:
        hotel_reserve_set.append(item.hotel_id)
    reserve = {
        'flight': flight_reserve_set,
        'bus': bus_reserve,
        'hotel': hotel_reserve
    }
    return reserve

# make reservation


def make_flight_reserve(user_id, flight_id):
    # check flight
    flights = Flight.objects.filter(flight_id=flight_id)
    if flights.count() == 0:
        return '_no_this_service'
    flight = flights[0]
    # check vacant
    reserved_count = flight.reserve.count()
    if reserved_count > flight.volume:
        return '_has_no_vacant'
    # check user
    users = User.objects.filter(id=user_id)
    if users.count() == 0:
        return '_no_this_user'
    user = users[0]
    # check reserve
    has_reserved = flight.reserve.filter(id=user_id).count()
    if has_reserved > 0:
        return '_has_reserved'
    # make reserve
    flight.reserve.add(user)
    flight.save()
    return 'ok'


def make_bus_reserve(user_id, bus_id):
    # check bus
    buses = Bus.objects.filter(bus_id=bus_id)
    if buses.count() == 0:
        return '_no_this_service'
    bus = buses[0]
    # check reserve
    reserved_count = bus.reserve.count()
    if reserved_count > bus.volume:
        return '_has_no_vacant'
    # check user
    users = User.objects.filter(id=user_id)
    if users.count() == 0:
        return '_no_this_user'
    user = users[0]
    # check reserve
    has_reserved = bus.reserve.filter(id=user_id).count()
    if has_reserved > 0:
        return '_has_reserved'
    # make reserve
    bus.reserve.add(user)
    bus.save()
    return 'ok'


def make_hotel_reserve(user_id, hotel_id):
    # check hotel
    hotels = Hotel.objects.filter(hotel_id=hotel_id)
    if hotels.count() == 0:
        return '_no_this_service'
    hotel = hotels[0]
    # check reserve
    reserved_count = hotel.reserve.count()
    if reserved_count > hotel.volume:
        return '_has_no_vacant'
    # check user
    users = User.objects.filter(id=user_id)
    if users.count() == 0:
        return '_no_this_user'
    user = users[0]
    # check reserve
    has_reserved = hotel.reserve.filter(id=user_id).count()
    if has_reserved > 0:
        return '_has_reserved'
    # make reserve
    hotel.reserve.add(user)
    hotel.save()
    return 'ok'


def cancel_flight_reserve(user_id, flight_id):
    # check flight
    flights = Flight.objects.filter(flight_id=flight_id)
    if flights.count() == 0:
        return '_no_this_service'
    flight = flights[0]
    # check user
    users = User.objects.filter(id=user_id)
    if users.count() == 0:
        return '_no_this_user'
    user = users[0]
    # check reserve
    reserves = flight.reserve.filter(id=user_id)
    if reserves.count() == 0:
        return '_no_this_reserve'
    # cancel reserve
    flight.reserve.remove(user)
    flight.save()
    return 'ok'


def cancel_bus_reserve(user_id, bus_id):
    # check bus
    buses = Bus.objects.filter(bus_id=bus_id)
    if buses.count() == 0:
        return '_no_this_service'
    bus = buses[0]
    # check user
    users = User.objects.filter(id=user_id)
    if users.count() == 0:
        return '_no_this_user'
    user = users[0]
    # check reserve
    reserves = bus.reserve.filter(id=user_id)
    if reserves.count() == 0:
        return '_no_this_reserve'
    # cancel reserve
    bus.reserve.remove(user)
    bus.save()
    return 'ok'


def cancel_hotel_reserve(user_id, hotel_id):
    # check hotel
    hotels = Hotel.objects.filter(hotel_id=hotel_id)
    if hotels.count() == 0:
        return '_no_this_service'
    hotel = hotels[0]
    # check user
    users = User.objects.filter(id=user_id)
    if users.count() == 0:
        return '_no_this_user'
    user = users[0]
    # check reserve
    reserves = hotel.reserve.filter(id=user_id)
    if reserves.count() == 0:
        return '_no_this_reserve'
    # cancel reserve
    hotel.reserve.remove(user)
    hotel.save()
    return 'ok'
